package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.commons.result.AbstractResponse;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author why
 * @since 2022/07/30 22:05
 */
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        return null;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {

        AllProductResponse allProductResponse = new AllProductResponse();

        PageHelper.startPage(request.getPage(),request.getSize());


        try {
            if (request.getSort().equals("1")) {
                List<Item> items = itemMapper.selectItemFront(request.getCid(), "price", "ASC"
                        , request.getPriceGt(), request.getPriceLte());
                List<ProductDto> productDtos = productConverter.items2Dto(items);
                allProductResponse.setProductDtoList(productDtos);
                long size = productDtos.size();
                allProductResponse.setTotal(size);

            } else if (request.getSort().equals("-1")) {
                List<Item> items = itemMapper.selectItemFront(request.getCid(), "price", "desc"
                        , request.getPriceGt(), request.getPriceLte());
                List<ProductDto> productDtos = productConverter.items2Dto(items);
                allProductResponse.setProductDtoList(productDtos);
                long size = productDtos.size();
                allProductResponse.setTotal(size);
            } else {
                List<Item> items = itemMapper.selectItemFront(request.getCid(), "created", "desc"
                        , request.getPriceGt(), request.getPriceLte());
                List<ProductDto> productDtos = productConverter.items2Dto(items);
                allProductResponse.setProductDtoList(productDtos);
                long size = productDtos.size();
                allProductResponse.setTotal(size);
            }

//            long i = itemMapper.selectCount(null);
//            allProductResponse.setTotal(i);

            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse,e);
        }

        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();

        try {
            PanelDto panelDto = new PanelDto();
            Example example = new Example(Panel.class);
            example.createCriteria().andEqualTo("name", "热门推荐");
            Panel panel = panelMapper.selectOneByExample(example);
            BeanUtils.copyProperties(panel, panelDto);
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
//        ArrayList<PanelContentItemDto> panelContentItemDtos = new ArrayList<>();
//        for (PanelContentItem panelContentItem : panelContentItems) {
//            PanelContentItemDto panelContentItemDto = new PanelContentItemDto();
//            BeanUtils.copyProperties(panelContentItem,panelContentItemDto);
//            panelContentItemDtos.add(panelContentItemDto);
//        }
//
//        panelDto.setPanelContentItems(panelContentItemDtos);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            HashSet<PanelDto> panelDtos = new HashSet<>();
            panelDtos.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (BeansException e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }


        return recommendResponse;
    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}
